﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Smartmanufact
{
    public partial class Cargo_View : Form
    {
        public static string shelfname = "";
        public static int row_index = -1;
        public static int col_index = -1;



        public Cargo_View()
        {
            InitializeComponent();
            // 把货架信息读进入
            try
            {
                DataSet ds = MainView.mysql.sql_search_database("Select * from entrepotinfo");
                int enterpotinfo_count = ds.Tables[0].Rows.Count;
                for (int i = 0; i < enterpotinfo_count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    comboBox1.Items.Add(dr[0].ToString());
                }
            }
            catch { }
        }

        private void Cargo_View_Load(object sender, EventArgs e)
        {
            
        }

        public void Reflush_View()
        {
             // 刷新页面
            try 
            { 
                // 读入货物信息
                DataSet ds = MainView.mysql.sql_search_database("Select * from cargotable where shelf='"+comboBox1.Text.Trim()+"'");
                int count = ds.Tables[0].Rows.Count;
                for(int i=0;i<count;i++)
                {
                    try
                    {
                        int row_index = int.Parse(ds.Tables[0].Rows[i][6].ToString());
                        int col_index = int.Parse(ds.Tables[0].Rows[i][7].ToString());
                        foreach(Control mycontrol in panel1.Controls)
                        {
                            Frame myfram = (Frame)mycontrol;
                            if(myfram.row_index==row_index && myfram.col_index==col_index)
                            {
                                myfram.empty_is = false;
                                myfram.BackColor = System.Drawing.Color.YellowGreen;
                                break;
                            }
                        }
                    }
                    catch { }
                }
            }
            catch
            {

            }
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                // 当下拉框中的文字发生改变的时候
                panel1.Controls.Clear();                 //将画面上的所有框都消除
                string name = comboBox1.Text.Trim();
                DataSet ds = MainView.mysql.sql_search_database("select * from entrepotinfo where enterpotname='"+name+"'");
                int row_count =int.Parse(ds.Tables[0].Rows[0][2].ToString());
                int col_count = int.Parse(ds.Tables[0].Rows[0][1].ToString());
                for (int i = 0; i < row_count; i++)
                {
                    for(int j=0;j<col_count;j++)
                    {
                        Frame myframe = new Frame();
                        myframe.Name = "frame_"+row_count.ToString()+"_"+col_count.ToString();
                        myframe.row_index = i;
                        myframe.col_index = j;
                        panel1.Controls.Add(myframe);
                        myframe.Left = j * myframe.Width;
                        myframe.Top = i * myframe.Height;
                        myframe.shelf = comboBox1.Text;
                    }
                }
                Reflush_View();
            }
            catch { }
        }

        private void Cargo_View_MouseMove(object sender, MouseEventArgs e)
        {
           
        }

        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                comboBox1.Items.Clear();
                comboBox1.Text = "";
                DataSet ds = MainView.mysql.sql_search_database("Select * from entrepotinfo");
                int enterpotinfo_count = ds.Tables[0].Rows.Count;
                for (int i = 0; i < enterpotinfo_count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    comboBox1.Items.Add(dr[0].ToString());
                }
                
            }
            catch { }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Xiadan xiadan = new Xiadan();
            DialogResult result = xiadan.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            chukuxiadan chuku = new chukuxiadan();
            DialogResult result = chuku.ShowDialog();
        }
    }
}
